package com.litign.personcontrol.controller;

import com.litign.personcontrol.pojo.Emp;
import com.litign.personcontrol.pojo.PageBean;
import com.litign.personcontrol.pojo.Result;
import com.litign.personcontrol.service.EmpService;
import com.litign.personcontrol.service.impl.EmpServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


@RestController
@RequestMapping("/emps")
public class EmpController {

    private static final Logger log = LoggerFactory.getLogger(EmpController.class);
    private final EmpServiceImpl empServiceImpl;

    public EmpController(EmpServiceImpl empServiceImpl) {
        this.empServiceImpl = empServiceImpl;
    }

    @Autowired
    private EmpService empService;

    //因为在类上加过了，所以原来需要加的就不需要了，只需要加个注解就行了
    @GetMapping
    public Result page(Integer page, Integer pageSize, String name, Short gender, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        //下面这两行是设置默认值
        if(page == null || page < 1)page =1;
        if(pageSize == null )pageSize = 10;

        log.info("分页查询，参数：{},{}",page,pageSize);
        //调用service分页查询
        PageBean pageBean = empService.page(page,pageSize, name,gender, begin, end);
        return Result.success(pageBean);


    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工，emp{}",emp);
        empService.save(emp);

        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findAll(@PathVariable Integer id){
        log.info("根据id查询员工信息：{}",id);
        Emp emp =  empService.getById(id);

        return Result.success(emp);
    }


    //加上requestbody是为了将json数据格式封装到实体类当中
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工emp{}",emp);
        empService.update(emp);
        return Result.success();
    }
}
